#include "Base.h"
#include "Server.h"
#include "DBPlayer.h"
#include "Log.h"
#include "LobbyProtocol.h"
#include "DBProtocol.h"
#include "LobbyServer.h"

CDBPlayer *g_pDBPlayer = NULL;

CDBPlayer::CDBPlayer(void)
{
}

CDBPlayer::~CDBPlayer(void)
{
}

BOOL CDBPlayer::OnInitialUpdate(const char *szServerIP, int nPort, int nOrigin, int nServerId)
{
	if(!CServerPlayer::OnInitialUpdate(szServerIP, nPort, nOrigin, nServerId))
	{
		WriteLog("%s : %d CDBPlayer::OnInitialUpdate return FALSE", __FILE__, __LINE__);
		return FALSE;
	}
	g_pDBPlayer = this;

	return TRUE;
}

BOOL CDBPlayer::ReqRegisterUser(Msg *pMsg, CLobbyClientPlayer *pPlayer)
{
	if(pMsg == NULL)
		return FALSE;
	if(pPlayer == NULL || pPlayer->GetClient() == NULL)
		return FALSE;
	CLRegisterUserMsg *pReq = (CLRegisterUserMsg *)pMsg;
	
	LDCreateUserMsg msg;
	msg.m_dwClientId = pPlayer->GetClient()->GetClientId();
    strncpy(msg.m_szUserName, pReq->m_szUserName, sizeof(msg.m_szUserName));
	strncpy(msg.m_szPwd, pReq->m_szPwd, sizeof(msg.m_szPwd));
	msg.m_nSex = pReq->m_nSex;
	strncpy(msg.m_szFullName, pReq->m_szFullName, sizeof(msg.m_szFullName));
	strncpy(msg.m_szIdentityCard,pReq->m_szIdentityCard,sizeof(msg.m_szIdentityCard));
	strncpy(msg.m_szBirthday, pReq->m_szBirthday, sizeof(msg.m_szBirthday));
	strncpy(msg.m_szCountry, pReq->m_szCountry, sizeof(msg.m_szCountry));
	strncpy(msg.m_szProvince, pReq->m_szProvince, sizeof(msg.m_szProvince));
	strncpy(msg.m_szEmail, pReq->m_szEmail, sizeof(msg.m_szEmail));
	msg.m_nFigure = pReq->m_nFigure;
	return SendMsg(&msg);
}

BOOL CDBPlayer::ReqUpdateUser(Msg *pMsg, CLobbyClientPlayer *pPlayer)
{
	if(pMsg == NULL)
		return FALSE;
	if(pPlayer == NULL || pPlayer->GetClient() == NULL)
		return FALSE;
	CLUpdateUserMsg *pReq = (CLUpdateUserMsg *)pMsg;

	LDUpdateUserMsg msg;
	msg.m_dwClientId = pPlayer->GetClient()->GetClientId();
	strncpy(msg.m_szUserName, pReq->m_szUserName, sizeof(msg.m_szUserName));
	msg.m_nSex = pReq->m_nSex;
	strncpy(msg.m_szFullName, pReq->m_szFullName, sizeof(msg.m_szFullName));
	strncpy(msg.m_szIdentityCard,pReq->m_szIdentityCard,sizeof(msg.m_szIdentityCard));
	strncpy(msg.m_szBirthday, pReq->m_szBirthday, sizeof(msg.m_szBirthday));
	strncpy(msg.m_szCountry, pReq->m_szCountry, sizeof(msg.m_szCountry));
	strncpy(msg.m_szProvince, pReq->m_szProvince, sizeof(msg.m_szProvince));
	strncpy(msg.m_szEmail, pReq->m_szEmail, sizeof(msg.m_szEmail));
    msg.m_nFigure = pReq->m_nFigure;
	return SendMsg(&msg);
}

BOOL CDBPlayer::ReqUpdatePassword(Msg *pMsg, CLobbyClientPlayer *pPlayer)
{
	if(pMsg == NULL)
		return FALSE;
	if(pPlayer == NULL || pPlayer->GetClient() == NULL)
		return FALSE;
	CLUpdatePasswordMsg *pReq = (CLUpdatePasswordMsg *)pMsg;

	LDUpdatePasswordMsg msg;
	msg.m_dwClientId = pPlayer->GetClient()->GetClientId();
	strncpy(msg.m_szUserName, pReq->m_szUserName, sizeof(msg.m_szUserName));
	strncpy(msg.m_szPwd, pReq->m_szOldPwd, sizeof(msg.m_szPwd));
	return SendMsg(&msg);
}

BOOL CDBPlayer::ReqUserLogin(Msg *pMsg, CLobbyClientPlayer *pPlayer)
{
	if(pMsg == NULL)
		return FALSE;
	if(pPlayer == NULL || pPlayer->GetClient() == NULL)
		return FALSE;
	CLUserLoginMsg *pReq = (CLUserLoginMsg *)pMsg;

	LDLoadUserMsg msg;
	msg.m_dwClientId = pPlayer->GetClient()->GetClientId();
	strncpy(msg.m_szUserName, pPlayer->GetUserName(), sizeof(msg.m_szUserName));
	return SendMsg(&msg);
}

Msg* CDBPlayer::CreateMsg(DWORD dwMsgId, int nLen)
{
	switch(dwMsgId)
	{
	case Msg_DL_Create_User:
			return new DLCreateUserMsg;
	case Msg_DL_Update_User:
			return new DLUpdateUserMsg;
	case Msg_DL_Update_Password:
			return new DLUpdatePasswordMsg;
	case Msg_DL_Load_User:
			return new DLLoadUserMsg;
	case Msg_DL_Sync_Update_User:
		    return new DLSyncUpdateUserMsg;
	default:
		return NULL;
	}
	return NULL;
}

BOOL CDBPlayer::OnMsg(Msg *pMsg)
{
	DWORD dwMsgId = pMsg->GetMsgId();
	switch(dwMsgId)
	{
	case Msg_DL_Create_User:
		{
			return AckRegisterUser(pMsg); 
		}
	case Msg_DL_Update_User:
		{
			return AckUpdateUser(pMsg);
		}
	case Msg_DL_Update_Password:
		{
			return AckUpdatePassword(pMsg);
		}
	case Msg_DL_Load_User:
		{
			return AckLoadUser(pMsg);
		}
	case Msg_DL_Sync_Update_User:
		{
			return OnSyncUpdateUser(pMsg);
		}
	default:
		return TRUE;
	}
	return TRUE;
}

BOOL CDBPlayer::AckRegisterUser(Msg *pMsg)
{
	DLCreateUserMsg *pAck = (DLCreateUserMsg *)pMsg;
	CLobbyClientPlayer *pPlayer = (CLobbyClientPlayer *)g_pServer->GetPlayerByClientId(pAck->GetClientId());
	if(pPlayer == NULL)
		return TRUE;
	pPlayer->OnAckRegisterUser(pAck);
	return TRUE;
}

BOOL CDBPlayer::AckUpdateUser(Msg *pMsg)
{
	DLUpdateUserMsg *pAck = (DLUpdateUserMsg *)pMsg;
	CLobbyClientPlayer *pPlayer = (CLobbyClientPlayer *)g_pServer->GetPlayerByClientId(pAck->GetClientId());
	if(pPlayer == NULL)
		return TRUE;
	pPlayer->OnAckUpdateUser(pAck);
	return TRUE;
}

BOOL CDBPlayer::AckUpdatePassword(Msg *pMsg)
{
	DLUpdatePasswordMsg *pAck = (DLUpdatePasswordMsg *)pMsg;
	CLobbyClientPlayer *pPlayer = (CLobbyClientPlayer *)g_pServer->GetPlayerByClientId(pAck->GetClientId());
	if(pPlayer == NULL)
		return TRUE;
	pPlayer->OnAckUpdatePassword(pAck);
	return TRUE;
}

BOOL CDBPlayer::AckLoadUser(Msg *pMsg)
{
    DLLoadUserMsg *pAck = (DLLoadUserMsg *)pMsg;
	CLobbyClientPlayer *pPlayer = (CLobbyClientPlayer *)g_pServer->GetPlayerByClientId(pAck->GetClientId());
	if(pPlayer == NULL)
		return TRUE;
    pPlayer->OnAckUserLogin(pAck);
	return TRUE;
}

BOOL CDBPlayer::OnSyncUpdateUser(Msg *pMsg)
{
	DLSyncUpdateUserMsg *pSyncMsg = (DLSyncUpdateUserMsg *)pMsg;
	CLobbyClientPlayer *pClientPlayer = g_pLobbyServer->FindClientPlayer(pSyncMsg->m_dwUserId);
	if(pClientPlayer == NULL)
	{
		return TRUE;
	}

	LCSyncUpdateUserMsg msg;
	msg.m_listItem.CloneFrom(pSyncMsg->m_listItem);
	msg.m_listUserAttr.CloneFrom(pSyncMsg->m_listUserAttr);
	pClientPlayer->SendMsg(&msg);
	return TRUE;
}